package com.car.serve.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.serve.Param.addTypeParam;
import com.car.serve.dto.ServeTypeDto;
import com.car.serve.entity.ServeType;
import com.car.serve.exception.ServeException;
import com.car.serve.form.PageServeTypeForm;
import com.car.serve.form.addTypeForm;
import com.car.serve.service.ServeTypeService;
import com.car.commons.core.model.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@RestController
@RequestMapping("/serveType")
@Api(tags = "服务类别信息")
public class ServeTypeController {

    @Resource
    private ServeTypeService serveTypeService;

    //新增服务类别
    @PostMapping("/addType.pt")
    @ApiOperation(value = "新增服务类别成功")
    public Result addType(@RequestBody addTypeForm form)throws Exception{
        serveTypeService.add(BeanUtil.toBean(form, addTypeParam.class));
        return Result.getResult().setMsg("新增服务类别成功");

    }
    //根据ID查询服务类别
    @GetMapping("/ByServeId")
    @ApiOperation(value = "根据ID查询服务类别")
    public Result<ServeTypeDto> ByServeId(Integer serveTypeId){
        ServeType type = serveTypeService.getById(serveTypeId);
        if (type==null)throw new ServeException(3000,"服务类别不存在");
        return Result.getResult(ServeTypeDto.class).setMsg("根据ID查询服务类别成功").setData(BeanUtil.toBean(type,ServeTypeDto.class));
    }

    @PostMapping("/findServeTypePage.pt")
    @ApiOperation(value = "分页查询服务类别")
    public Result findServeTypePage(@RequestBody PageServeTypeForm form)throws Exception{
        Page<ServeTypeDto> serveTypePage = serveTypeService.findServeTypePage(form);
        return Result.getResult().setMsg("分页查询成功").setData(serveTypePage);
    }

    @GetMapping("/getMyServeType.pt")
    @ApiOperation(value = "查询服务类别")
    public Result getMyServeType()throws Exception{
        List<ServeType> myServeType = serveTypeService.getMyServeType();
        return Result.getResult().setMsg("查询类别成功").setData(myServeType);
    }






}

